<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <select class="province">
        <option value="">请选择省份</option>
        <!-- <option value="A001">北京</option>
        <option value="A002">上海</option>
        <option value="A003">湖北</option>
        <option value="A004">湖南</option> -->
    </select>
    <select class="city" name="" id="">
        <option value="">请选择市/区</option>
        <!-- <option value="A001">朝阳</option>
        <option value="A002">海淀</option>
        <option value="A003">沙河</option>
        <option value="A004">上地</option> -->
    </select>
</body>
<script>
    // 需求
    // 1. 页面加载时 根据数据 生成省份相关的信息 => 放到省份的下拉框
    // 2. 省份的下拉框 切换时 => 从所有数据中找到 当前省份的数据 => 获取省份下的市区集合 => 生成生成市区相关的信息 => 放到市区的下拉框
    // 3.  市区的下拉框 切换时 => 从所有数据中找到 当前市区的数据 => 获取市区下的县/镇集合 => 生成生成县/镇相关的信息 => 放到县/镇的下拉框

    var proSel = document.getElementsByClassName("province")[0];
    var citySel = document.getElementsByClassName("city")[0];

    var xhr = new XMLHttpRequest(); // 0

    xhr.open("get", "../data/pro.json", true);  // 1

    xhr.send();  // 1

    xhr.onreadystatechange = function () {
        // 2  3  4
        console.log("readyState:", xhr.readyState);
        if (xhr.readyState == 4 && xhr.status == 200) {
            var result = xhr.responseText;
            result = JSON.parse(result);
            console.log(result);

            var { list: proList } = result;
            console.log(proList);

            // var html = "";
            // proList.forEach(function (item) {
            //     console.log(item);
            //     var { city_id, city_name } = item;
            //     html += `<option value="${city_id}">${city_name}</option>`;
            // })
            // proSel.innerHTML = html;


            // var html = "";
            // proList.forEach(function ({ city_id, city_name }, index) {
            //     html += `<option value="${city_id}">${city_name}</option>`;
            // })
            // proSel.innerHTML = html;

            // var html = "";
            // proList.forEach((item, index) => html += `<option value="${city_id}">${city_name}</option>`
            // )
            // proSel.innerHTML = html;


            var html = `<option value="">请选择省份</option>`;
            proList.forEach(({ city_id, city_name }, index) => html += `<option value="${city_id}">${city_name}</option>`);
            proSel.innerHTML = html;


            proSel.onchange = function () {
                var cityId = this.value;
                console.log(cityId, proList);

                if (cityId) {
                    // var thisPro = proList.find((item) => item.city_id == cityId);
                    var thisPro = proList.find(({ city_id, city_name }) => city_id === cityId);
                    console.log(thisPro);

                    var { list: cityList } = thisPro;

                    var html = `<option value="">请选择市/区</option>`;
                    cityList.forEach(({ city_id, city_name }, index) => html += `<option value="${city_id}">${city_name}</option>`);
                    citySel.innerHTML = html;


                    // citySel.onchange = function () {

                    // }


                } else {
                    citySel.innerHTML = `<option value="">请选择市/区</option>`;
                }

            }


        }
    }








</script>

</html>